package cn.tangsihai.hrm.handler;

import cn.tangsihai.hrm.util.AjaxResult;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

// 认证结果成功处理
@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        AjaxResult ajaxResult = AjaxResult.me().setSuccess(true).setMessage("认证成功").setResultObj(authentication.getAuthorities().toString());
        response.getWriter().print(JSON.toJSONString(ajaxResult));
        response.getWriter().flush();
        response.getWriter().close();
    }
}
